//
// Created by wuzhe on 2022/4/27.
//

#include "Slave.h"

Slave::Slave(int num): Entity(num, EntityType::SLAVE),space(Space::getSingleton()) {
    slaveState = new SlaveOfflineState(*this);
    INFO(std::string("创建slave server entity:") + std::to_string(num));
    if (isMain) {
        space.send2Slave(this, (char)ACK);
    }
}

Entity *Slave::process(ParseRes &res) {
    switch (res.head->type) {
        case ACK: {
            slaveState->getAck();
            break;
        }
        case SEND_ROOM: {
            slaveState->getSendRoom(res.content);
            break;
        }
        default: {

        }
    }
    return nullptr;
}

void Slave::sendRoomId(std::string roomId) {
    space.send2Slave(this, (char)SEND_ROOM, roomId);
}

void Slave::createRoom(std::string roomName) {
    RedisAsync *redis = RedisAsync::getSingleton();
    redis->get(roomName, [=](std::string roomId) {
        if (roomId == "nil") {
            return;
        }
        this->space.instRoom(roomName, std::stoi(roomId));
    });
}
